library(tidyverse)
library(lubridate)
library(data.table)
library(ggthemes)
library(glue)

theme_set(theme_few())

base_dir <- ".."
plot_dir <- glue("{base_dir}/figures")
data_dir <- glue("{base_dir}/data")
output_dir <- glue("{base_dir}/output")


raw_transcripts <- readRDS(glue("{output_dir}/raw_transcripts.rds"))


### MENTIONS OF THE LITERAL PHRASE "Tea Party"

all_tp_mentions <- raw_transcripts %>%
  .[, tea_party := str_count(text, pattern = regex("Tea Party", ignore_case = T))] %>%
  .[, .(tea_party = sum(tea_party)), by = .(channel, date)] %>%
  .[CJ(channel = channel, date = date, unique = T),
    on = .(channel, date)] %>%
  setnafill(fill=0, cols = "tea_party") %>%
  setkey(channel, date)

# FIGURE D.1.1

all_tp_mentions_wk <- copy(all_tp_mentions) %>%
  .[, c("week", "year") := .(week(date), year(date))] %>%
  .[, date := min(date), by = .(year, week)] %>%
  .[, .(tea_party = sum(tea_party)), by = .(channel, date)]

all_tp_mentions_wk[, channel := recode(channel, cnn = "CNN", fox = "FNC", msnbc = "MSNBC")]

ggplot(aes(x = date, y = tea_party, group = channel), data = all_tp_mentions_wk[date %between% c(mdy("2/11/2009", "6/03/2009"))]) +
  geom_bar(aes(fill = channel), stat = "identity", position = "dodge") +
  scale_fill_manual(values=c(CNN="mediumpurple2", FNC="darkred", MSNBC="cornflowerblue")) +
  scale_x_date(breaks = ymd(c("2009-2-15", "2009-3-01", "2009-3-15", "2009-4-1", "2009-4-15", "2009-5-1", "2009-5-15", "2009-6-1")), date_labels = "%b %e") +
  labs(x = "Date", y = "Count of Phrase 'Tea Party'", fill = "Channel")

ggsave(glue("{plot_dir}/tp_literal_mentions_weekly.pdf"), width=8, height=5)


### MENTIONS OF THE LITERAL PHRASE "Occupy"

all_occupy_mentions <- raw_transcripts %>%
  .[, occupy := str_count(text, pattern = regex("Occupy", ignore_case = T))] %>%
  .[, .(occupy = sum(occupy)), by = .(channel, date)] %>%
  .[CJ(channel = channel, date = date, unique = T),
    on = .(channel, date)] %>%
  setnafill(fill=0, cols = "occupy") %>%
  setkey(channel, date)

# FIGURE I.1

all_occupy_mentions_wk <- copy(all_occupy_mentions) %>%
  .[, c("week", "year") := .(week(date), year(date))] %>%
  .[, date := min(date), by = .(year, week)] %>%
  .[, .(occupy = sum(occupy)), by = .(channel, date)]

all_occupy_mentions_wk[, channel := recode(channel, cnn = "CNN", fox = "FNC", msnbc = "MSNBC")]


ggplot(aes(x = date, y = occupy, group = channel), data = all_occupy_mentions_wk[date %between% c(mdy("8/1/2011", "12/31/2011"))]) +
  geom_bar(aes(fill = channel), stat = "identity", position = "dodge") +
  scale_fill_manual(values=c(CNN="mediumpurple2", FNC="darkred", MSNBC="cornflowerblue")) +
  scale_x_date(breaks = ymd(c("2011-08-01", "2011-08-15", "2011-09-01", "2011-09-15", "2011-10-01", "2011-10-15", "2011-11-01", "2011-11-15", "2011-12-01", "2011-12-15")), date_labels = "%b %e") +
  labs(x = "Date", y = "Weekly Count of Phrase 'Occupy'", fill = "Channel")
ggsave(glue("{plot_dir}/occupy_literal_mentions_weekly.pdf"), width=8, height=5)
